/*
--------------------------------------------------------------------------------
				Ak-Lib. 
			 Project       	: Ak-Lib
			 Group         	: Ak-Lib R&D Team
			 Author        	: Ocean.Wang
			 Create Date 	: 05-01-2010
--------------------------------------------------------------------------------
Code Name 	:  A.K 	~v~
Description	:  Yet Another Cross-Platform Lib.
Cautions    	:
--------------------------------------------------------------------------------
Change History:
Version		: 0.1
Building 		: 05/01/10
10-05.01 first draft, build, debug and test.
--------------------------------------------------------------------------------
*/

#include "ak_step.h"

enum{
	EVT_TO_STEP0,
	EVT_TO_STEP1,
	EVT_TO_STEP2,
	EVT_TO_STEP3
}_Test_Evt;


static 	int Step0_Do(_t_sProcNode * pNode, void * pArg)
{
	printf("Step0 Do: (%p)\n", pNode);
	return STEP_RET_WAIT;
}

static 	int Step0_Undo(_t_sProcNode * pNode, void * pArg)
{
	printf("Step0 Undo: (%p)\n", pNode);
	return 0;
}

static 	int Step0_EvtHdl(_t_sProcNode * pNode, void * pArg, _t_sEvent * pEvent)
{
	printf("Step0 EvtHdl: (%p) - Event:(%p)\n", pNode, pEvent);
	return STEP_RET_SUCC;
}

static 	int Step0_TimeOut(_t_sProcNode * pNode, void * pArg)
{
	printf("Step0 TimeOut: (%p)\n", pNode);
	return 0;
}


static 	_t_sStepDesc 	t_Step0={
	1000,
	(int (*)())Step0_Do,
	(int (*)())Step0_Undo,
	(int (*)())Step0_EvtHdl,
	(int (*)())Step0_TimeOut
};


static 	int Step1_Do(_t_sProcNode * pNode, void * pArg)
{
	printf("Step1 Do: (%p)\n", pNode);
	return STEP_RET_WAIT;
}

static 	int Step1_Undo(_t_sProcNode * pNode, void * pArg)
{
	printf("Step1 Undo: (%p)\n", pNode);
	return 0;
}

static 	int Step1_EvtHdl(_t_sProcNode * pNode, void * pArg, _t_sEvent * pEvent)
{
	printf("Step0 EvtHdl: (%p) - Event:(%p)\n", pNode, pEvent);
	return STEP_RET_WAIT;
}

static 	int Step1_TimeOut(_t_sProcNode * pNode, void * pArg)
{
	printf("Step1 TimeOut: (%p)\n", pNode);
	return STEP_RET_SUCC;
}


static 	_t_sStepDesc 	t_Step1={
	1000,
	(int (*)())Step1_Do,
	(int (*)())Step1_Undo,
	(int (*)())Step1_EvtHdl,
	(int (*)())Step1_TimeOut
};


static 	int Step2_Do(_t_sProcNode * pNode, void * pArg)
{
	printf("Step2 Do: (%p)\n", pNode);
	return STEP_RET_WAIT;
}

static 	int Step2_Undo(_t_sProcNode * pNode, void * pArg)
{
	printf("Step2 Undo: (%p)\n", pNode);
	return 0;
}

static 	int Step2_EvtHdl(_t_sProcNode * pNode, void * pArg, _t_sEvent * pEvent)
{
	printf("Step0 EvtHdl: (%p) - Event:(%p)\n", pNode, pEvent);
	return STEP_RET_SUCC;
}

static 	int Step2_TimeOut(_t_sProcNode * pNode,  void * pArg)
{
	printf("Step2 TimeOut: (%p)\n", pNode);
	return STEP_RET_FAIL;
}


static 	_t_sStepDesc 	t_Step2={
	1000,
	(int (*)())Step2_Do,
	(int (*)())Step2_Undo,
	(int (*)())Step2_EvtHdl,
	(int (*)())Step2_TimeOut
};


static 	int Step3_Do(_t_sProcNode * pNode, void * pArg)
{
	printf("Step3 Do: (%p)\n", pNode);
	return STEP_RET_WAIT;
}

static 	int Step3_Undo(_t_sProcNode * pNode, void * pArg)
{
	printf("Step3 Undo: (%p)\n", pNode);
	return 0;
}

static 	int Step3_EvtHdl(_t_sProcNode * pNode, void * pArg, _t_sEvent * pEvent)
{
	printf("Step0 EvtHdl: (%p) - Event:(%p)\n", pNode, pEvent);
	return STEP_RET_WAIT;
}

static 	int Step3_TimeOut(_t_sProcNode * pNode, void * pArg)
{
	printf("Step3 TimeOut: (%p)\n", pNode);
	return STEP_RET_FAIL;
}


static 	_t_sStepDesc 	t_Step3={
	1000,
	(int (*)())Step3_Do,
	(int (*)())Step3_Undo,
	(int (*)())Step3_EvtHdl,
	(int (*)())Step3_TimeOut
};


static 	_t_sStepDesc *	t_Step[]={
	&t_Step0,
	&t_Step1,
	&t_Step2,
	&t_Step3
};

static 	int 	Proc0_Init(_t_sProcNode * pNode)
{
	printf("Init the Proc0: (%p)\n", pNode);
	return 0;
}

static 	int 	Proc0_Fini(_t_sProcNode * pNode, int state)
{
	printf("Fini the Proc0: %d\n", state);
	return 0;
}

static 	int 	Proc0_Exception(_t_sProcNode * pNode)
{
	printf("Exception the Proc0\n");
	return 0;
}

static 	_t_sProcDesc	t_proc0={
	0X10,	/* Proc Id, for Stat, 1-255 */
	_N_ARRAY(t_Step),	t_Step,
	(int (*)())Proc0_Init,
	(int (*)())Proc0_Fini,
	(int (*)())Proc0_Exception,
};



/* Init the Global Sche Context. Normally in the Global Init */
static 	_t_sProcSche  procSche = {0};	
static 	_t_sProcPool  t_ProcPool = {0};
static 	STimer_Svr 	  t_TimerSvr = {0};
static 	_t_sProcStat  t_ProcStat = {0};

static 	int Test_Proc(void)
{
	_t_sProcNode *	procNode;

	/* Global Init */
	SProcPool_Init(&t_ProcPool, 100);
	STimer_SvrStart(&t_TimerSvr);
	SProcSche_Init(&procSche, &t_ProcPool, &t_TimerSvr, &t_ProcStat);



	/* Normally User Only Need Do ProcNode Init & Sche */
	procNode = SProcNode_Init(&procSche, &t_proc0, NULL, SPROC_PRI_HIGH);
	SProcNode_Go(&procSche, procNode, STEP_OP_DO, NULL);


	return 0;
}

int 	main(void)
{
	Test_Proc();
	do{
		SProcSche_EvtHdl(&procSche, NULL);
		SYS_SLEEP(250);
		STimer_Sche(&t_TimerSvr, 5);
	}while(1);
	
	return 0;
}


